package com.jsz.peini.controller.pay;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jsz.peini.bean.activity.ActivityInfo;
import com.jsz.peini.bean.order.ConversionDetail;
import com.jsz.peini.bean.order.ConversionDetailExample;
import com.jsz.peini.bean.order.ConversionInfo;
import com.jsz.peini.bean.order.ConversionInfoExample;
import com.jsz.peini.bean.order.GoldHistory;
import com.jsz.peini.bean.order.GoldHistoryExample;
import com.jsz.peini.bean.order.OrderInfo;
import com.jsz.peini.bean.order.OrderInfoExample;
import com.jsz.peini.bean.order.PayStream;
import com.jsz.peini.bean.seller.SellerInfo;
import com.jsz.peini.bean.task.PartyUser;
import com.jsz.peini.bean.task.PartyUserExample;
import com.jsz.peini.bean.task.TaskInfo;
import com.jsz.peini.bean.user.UserAccount;
import com.jsz.peini.bean.user.UserAccountExample;
import com.jsz.peini.bean.user.UserConcern;
import com.jsz.peini.bean.user.UserConcernExample;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.meta.OrderType;
import com.jsz.peini.common.util.AppPush;
import com.jsz.peini.common.util.AppSecretUtil;
import com.jsz.peini.common.util.DateTimeUtils;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.common.util.SmsSendUtil;
import com.jsz.peini.mapper.activity.ActivityInfoMapper;
import com.jsz.peini.mapper.order.ConversionDetailMapper;
import com.jsz.peini.mapper.order.ConversionInfoMapper;
import com.jsz.peini.mapper.order.GoldHistoryMapper;
import com.jsz.peini.mapper.order.OrderInfoMapper;
import com.jsz.peini.mapper.order.PayStreamMapper;
import com.jsz.peini.mapper.seller.SellerInfoMapper;
import com.jsz.peini.mapper.task.TaskInfoMapper;
import com.jsz.peini.mapper.user.UserAccountMapper;
import com.jsz.peini.mapper.user.UserConcernMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.mapper.user.UserSmsMapper;
import com.jsz.peini.redis.base.RedisConstants;
import com.jsz.peini.redis.base.RedisUtil;
import com.jsz.peini.redis.base.SerializeUtil;
import com.jsz.peini.redis.util.UserRedisUtil;
import com.jsz.peini.service.order.OrderInfoService;
import com.jsz.peini.service.task.PartyService;
import com.jsz.peini.vo.UserVo;

/**
 * 金币支付相关
 * @author Han Guohong
 *
 */
@Controller
@RequestMapping("/payGold")
public class PayByGoldController {
	private static final Logger logger = LoggerFactory.getLogger(PayByGoldController.class);
	@Resource
	private PayStreamMapper payStreamMapper;
	@Resource
	private OrderInfoMapper orderInfoMapper;
	@Resource
	private TaskInfoMapper taskInfoMapper;
	@Resource
	private UserSmsMapper userSmsMapper;
	@Resource
	private UserInfoMapper userInfoMapper;
	@Resource
	private OrderInfoService orderInfoService;
	@Resource
	private UserAccountMapper userAccountMapper;
	@Resource
	private GoldHistoryMapper goldHistoryMapper;
	@Resource
	private SellerInfoMapper sellerInfoMapper;
	@Resource
	private ConversionInfoMapper conversionInfoMapper;
	@Resource
	private ConversionDetailMapper conversionDetailMapper;
	@Resource
	private UserConcernMapper userConcernMapper;
	@Resource
	private ActivityInfoMapper activityInfoMapper;
	
	@Resource
	private PartyService partyService;
	
	/**
	 * 生成金币购买订单
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param userId
	 * @param localId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/saveBuyGoldOrder",produces="text/plain;charset=UTF-8")
	public String saveBuyGoldOrder(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="userId",required=true) String userId,//身份token
			@RequestParam(value="localId",required=true) Integer localId
			) throws Exception{
		JSONObject object = new JSONObject();
		try {
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj.toJSONString();
			}
			userId = obj.getString("userId");
			
			ConversionDetail conversionDetail = conversionDetailMapper.selectByPrimaryKey(localId);
			if(conversionDetail == null){
				object.put("resultCode",Constants.FAIL);
				object.put("resultDesc", "参数错误");
				return object.toString();
			}
			
			//生成金币购买订单
			OrderInfo orderInfo = new OrderInfo();
			
			orderInfo.setUserId(userId);
			orderInfo.setOrderType(6);
			orderInfo.setOrderStatus(0);
			orderInfo.setAllMoney((int)(conversionDetail.getPayNum()));
			orderInfo.setPayMoney((int)(conversionDetail.getPayNum()));
			orderInfo.setTaskInfoId(localId);//将规则id存入taskId
			
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
			orderInfo.setUserPhone(userInfo.getUserPhone());
			orderInfo.setOrderTime(new Date());
			
			orderInfoMapper.insert(orderInfo);
			Long orderId = orderInfo.getId();
			String code = "";
			code += orderInfo.getOrderType();
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
			code += sdf.format(new Date());
			String codeStr = String.valueOf(orderInfo.getId());
			if(codeStr.length()> 6){
				codeStr = codeStr.substring(codeStr.length()-6);
			}else if(codeStr.length() < 6){
				for (int i = 0; i < 6-codeStr.length(); i++) {
					code += "0";
				}
			}
			code += codeStr;
			orderInfo.setOrderCode(code);
			orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
			
			object.put("orderIdStr", orderId.toString());
	        object.put("resultCode",Constants.SUCCESS);
	        object.put("resultDesc","SUCCESS");
		} catch (Exception e) {
			object.put("resultCode",Constants.EXCEPTION);
			object.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
		}
		
		return object.toString();
	}
	
	/**
	 * 金币转赠
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param userId
	 * @param otherId
	 * @param token
	 * @param appA
	 * @param gold
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/donation",produces="text/plain;charset=UTF-8")
	public String donation(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="userId",required=true) String userId,//身份token
			@RequestParam(value="otherId",required=true) String otherId,
			@RequestParam(value="token",required=true) String token,//32位密钥
			@RequestParam(value="appA",required=true) String appA,  //交换参数
			@RequestParam(value="goldNum",required=true) BigDecimal gold,
			@RequestParam(value="content",required=true) String content
			) throws Exception{
		JSONObject object = new JSONObject();
		try {
			if(appA == null){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			//TODO 转账接口
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			if(StringUtils.isBlank(token)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			if(gold.doubleValue() <= 0){
				object.put("resultCode", Constants.FAIL);
				object.put("resultDesc", "转账金额必须大于0");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj.toJSONString();
			}
			userId = obj.getString("userId");
			
			if(AppSecretUtil.valAppToken(userId, token)){
				
			}else{
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			UserAccount account = userAccountMapper.selectByUserId(userId);
			
			if(StringUtils.isBlank(account.getPayPassword())){
				object.put("resultCode", Constants.NOPAYPASS);
				object.put("resultDesc", "您还没有设置支付密码");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);	
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			if(account.getGold() < gold.doubleValue()*100){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", "您的剩余金币不足以支付，请充值或选择其他方式付款！");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			//生成金币打赏订单
			OrderInfo orderInfo = new OrderInfo();
			
			//查询是否提交过订单
			/*OrderInfoExample example = new OrderInfoExample();
			com.jsz.peini.bean.order.OrderInfoExample.Criteria criteria = example.createCriteria();
			criteria.andUserIdEqualTo(userId);
			criteria.andOtherIdEqualTo(otherId);
			criteria.andOrderTypeEqualTo(5);
			criteria.andOrderStatusEqualTo(0);
			List<OrderInfo> orderInfos = orderInfoMapper.selectByExample(example);
			if(orderInfos.size()>0){
				orderInfo = orderInfos.get(0);
			}*/
			
			orderInfo.setUserId(userId);
			orderInfo.setOtherId(otherId);
			orderInfo.setOrderType(5);
			orderInfo.setOrderStatus(0);
			orderInfo.setAllMoney(gold.multiply(new BigDecimal("100")).intValue());
			orderInfo.setPayMoney(gold.multiply(new BigDecimal("100")).intValue());
			//orderInfo.setAllMoney((int)(gold*100));
			//orderInfo.setPayMoney((int)(gold*100));
			
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
			orderInfo.setUserPhone(userInfo.getUserPhone());
			orderInfo.setOrderTime(new Date());
			
			if(orderInfo.getId() != null && orderInfo.getId() != 0){
				
			}else{
				orderInfoMapper.insertSelective(orderInfo);
				Long orderId = orderInfo.getId();
				String code = "";
				code += orderInfo.getOrderType();
				SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
				code += sdf.format(new Date());
				String codeStr = String.valueOf(orderInfo.getId());
				if(codeStr.length()> 6){
					codeStr = codeStr.substring(codeStr.length()-6);
				}else if(codeStr.length() < 6){
					for (int i = 0; i < 6-codeStr.length(); i++) {
						code += "0";
					}
				}
				code += codeStr;
				orderInfo.setOrderCode(code);
			}
			
			orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
			
			PayStream payStream = new PayStream();
			String payId = "";
			
			payStream.setPayStatus(0);
			//payStream.setPayType(2);
			payStream.setUserId(userId);
			payStream.setPayTime(new Date());
			payStream.setOrderIdsStr(orderInfo.getId().toString());
			//payId = UUID.randomUUID().toString().replace("-", "");
			payId = orderInfo.getOrderCode();
			payStream.setPayId(payId);
			payStreamMapper.insertSelective(payStream);
			
			String bodyStr = "";
			String titleStr = "";
			Double totalPay = 0.00;
			int taskId = 0;
			int orderType = 0;
			
			titleStr = "金币转赠：";
			UserInfo otherInfo = userInfoMapper.selectByPrimaryKey(otherId);
			bodyStr = DateTimeUtils.formatDateTime2String(orderInfo.getOrderTime(),"yyyy-MM-dd HH:mm")+" "+otherInfo.getNickname();
			
			totalPay += gold.doubleValue();
			
			orderInfo.setPayType(1);
			orderInfo.setPayStatus(0);
			orderInfo.setPayStreamId(payId);
			orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
			if(titleStr.length()>100){
				titleStr = titleStr.substring(0, 100);
			}
			
			payStream.setPayMoney(totalPay);
			payStream.setPayCode(UUID.randomUUID().toString().replace("-", ""));
			payStreamMapper.updateByPrimaryKey(payStream);
			
			String serverB = AppSecretUtil.getAppSecret(userId, appA);			
			JSONObject dataObj = new JSONObject();	
			dataObj.put("serverB", serverB);			
			dataObj.put("payId", payId);
			object.put("data",dataObj);
			
	        object.put("resultCode",Constants.SUCCESS);
	        object.put("resultDesc","SUCCESS");
		} catch (Exception e) {
			object.put("resultCode",Constants.EXCEPTION);
			object.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
		}
		
		return object.toString();
	}
	
	/**
	 * 最近转账记录
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param userId
	 * @param token
	 * @param appA
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/recentDonation",produces="text/plain;charset=UTF-8")
	public String recentDonation(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="userId",required=true) String userId,//身份token
			@RequestParam(value="token",required=true) String token,//32位密钥
			@RequestParam(value="appA",required=true) String appA,  //交换参数
			@RequestParam(value="page",required=true) Integer page,
			@RequestParam(value="rows",required=true) Integer rows
			) throws Exception{
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		try {
			if(appA == null){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			if(StringUtils.isBlank(token)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();
				dataObj.put("serverB", serverB);			
				dataObj.put("list", array);
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj.toJSONString();
			}
			userId = obj.getString("userId");
			
			if(AppSecretUtil.valAppToken(userId, token)){
				
			}else{
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("list", array);
				object.put("data",dataObj);
				
				return object.toJSONString();
			}
			
			//查询订单
			
			HashMap<String, Object> map = new HashMap<String, Object>();
			
			Page<?> p = PageHelper.startPage(page,rows);
			GoldHistoryExample example = new GoldHistoryExample();
			example.createCriteria().andUserIdEqualTo(userId).andHisTypeBetween(7, 8).andIsDelEqualTo(1);
			example.setDistinct(false);
			example.setOrderByClause("create_time desc");
			List<GoldHistory> hisList = goldHistoryMapper.selectByExample(example);
			for (GoldHistory goldHistory : hisList) {
				String otherId = goldHistory.getOtherid();
				
				if(!map.containsKey(otherId)){
					UserInfo otherInfo = userInfoMapper.selectByPrimaryKey(otherId);
					JSONObject obj1 = new JSONObject();
					
					obj1.put("hisId", goldHistory.getHisId());
					obj1.put("otherId", otherId);
					obj1.put("headImg", otherInfo.getImageHead());
					obj1.put("nickName", PeiniUtils.updateNickName(otherInfo.getNickname(),otherInfo.getAccCode()));
					
					boolean flag1 = false;//我关注了他
					boolean flag2 = false;//他关注了我
					UserConcernExample example1 = new UserConcernExample();
					example1.or().andUserIdEqualTo(userId);
					example1.or().andOtherIdEqualTo(userId);
					List<UserConcern> list = userConcernMapper.selectByExample(example1);
					for (UserConcern userConcern : list) {
						if(userConcern.getUserId().equals(userId)){//我关注的人
							if(userConcern.getOtherId().equals(otherId)){
								flag1 = true;
							}
						}else{//我的粉丝
							if(userConcern.getUserId().equals(otherId)){
								flag2 = true;
							}
						}
					}
					if(flag1 == false && flag2 == false){
						obj1.put("relation", "陌生人");//陌生人
					}else if(flag1 == true && flag2 == true){
						obj1.put("relation", "双方关注");
					}else if(flag1 == true && flag2 == false){
						obj1.put("relation", "关注");
					}else if(flag1 == false && flag2 == true){
						obj1.put("relation", "粉丝");
					}
					array.add(obj1);
					
					map.put(otherId, 1);
				}else{
					//map.put(otherId, 1);
				}
			}
			
			String serverB = AppSecretUtil.getAppSecret(userId, appA);
			
			JSONObject dataObj = new JSONObject();
			
			dataObj.put("serverB", serverB);
			
			if(array.size() >= 3){
				dataObj.put("list", array.subList(0, 3));
			}else{
				dataObj.put("list", array);
			}
			
	        object.put("resultCode",Constants.SUCCESS);
	        object.put("resultDesc","SUCCESS");
	        object.put("data",dataObj);
		} catch (Exception e) {
			object.put("resultCode",Constants.EXCEPTION);
			object.put("resultDesc", Constants.EXCEPTION_DESC);
		}
		return object.toJSONString();
	}
	
	/**
	 * 最近金币转账消息删除
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param userId
	 * @param token
	 * @param appA
	 * @param hisId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/recentDonationDel",produces="text/plain;charset=UTF-8")
	public String recentDonationDel(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="userId",required=true) String userId,//身份token
			@RequestParam(value="token",required=true) String token,//32位密钥
			@RequestParam(value="appA",required=true) String appA,  //交换参数
			@RequestParam(value="hisId",required=true) String hisId
			) throws Exception{
		JSONObject object = new JSONObject();
		try {
			if(appA == null){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			if(StringUtils.isBlank(token)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj.toJSONString();
			}
			userId = obj.getString("userId");
			
			if(AppSecretUtil.valAppToken(userId, token)){
				
			}else{
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			GoldHistory history = goldHistoryMapper.selectByPrimaryKey(hisId);
			history.setIsDel(0);
			goldHistoryMapper.updateByPrimaryKey(history);
			
			String serverB = AppSecretUtil.getAppSecret(userId, appA);
			
			JSONObject dataObj = new JSONObject();
			
			dataObj.put("serverB", serverB);
			
	        object.put("resultCode",Constants.SUCCESS);
	        object.put("resultDesc","SUCCESS");
	        object.put("data",dataObj);
		} catch (Exception e) {
			object.put("resultCode",Constants.EXCEPTION);
			object.put("resultDesc", Constants.EXCEPTION_DESC);
		}
		
		return object.toString();
	}
	
	/**
	 * 发起支付
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/prePay",produces="text/plain;charset=UTF-8")
	public String appPre(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="orderIdStr",required=true) String orderIdStr,
			@RequestParam(value="token",required=true) String token,//32位密钥
			@RequestParam(value="userId",required=true) String userId,//身份token
			@RequestParam(value="appA",required=true) String appA  //交换参数
			) throws Exception{
		
		JSONObject object = new JSONObject();
		try {
			if(StringUtils.isEmpty(orderIdStr)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", "订单号不能为空");
				return object.toString();
			}
			if(appA == null){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			if(StringUtils.isBlank(token)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj.toJSONString();
			}
			userId = obj.getString("userId");
			//Reguser member = reguserService.findById(userId);
			
			if(AppSecretUtil.valAppToken(userId, token)){
				
			}else{
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			UserAccountExample example = new UserAccountExample();
			com.jsz.peini.bean.user.UserAccountExample.Criteria criteria = example.createCriteria();
			criteria.andUserIdEqualTo(userId);
			List<UserAccount> accounts = userAccountMapper.selectByExample(example);
			
			UserAccount userAccount = accounts.get(0);
			if(StringUtils.isBlank(userAccount.getPayPassword())){
				
				object.put("resultCode", Constants.NOPAYPASS);
				object.put("resultDesc", "您还没有设置支付密码");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				dataObj.put("payId", "");
				object.put("data",dataObj);
				
				return object.toJSONString();
			}
			
			if(orderIdStr.endsWith(",")){
				orderIdStr = orderIdStr.substring(0, orderIdStr.length()-1);
			}
			
			OrderInfo title = orderInfoMapper.selectByPrimaryKey(Long.parseLong(orderIdStr));
			String orderCode = title.getOrderCode();
			if(StringUtils.isBlank(orderCode)){
				orderCode = title.getId().toString();
			}
			
			PayStream payStream = new PayStream();
			String payId = "";
			
			payStream = payStreamMapper.selectByPrimaryKey(orderCode);
			
			if(payStream != null){//支付流水已经创建过 
				if(payStream.getPayStatus() == 1){
					object.put("resultCode", "0"); object.put("resultDesc","该订单已经支付过啦，请勿重复支付"); 
					
					String serverB = AppSecretUtil.getAppSecret(userId, appA);			
					JSONObject dataObj = new JSONObject();
					dataObj.put("serverB", serverB);
					dataObj.put("payId", "");
					object.put("data",dataObj);
					
					return object.toString(); 
				}else{
					payStream.setPayTime(new Date()); 
					payId = payStream.getPayId(); 
				}
			}else{
				payStream = new PayStream();
				payId = orderCode;
				payStream.setPayStatus(0);
				//payStream.setPayType(1);
				payStream.setUserId(userId);
				payStream.setPayTime(new Date());
				payStream.setOrderIdsStr(orderIdStr);
				//payId = UUID.randomUUID().toString().replace("-", "");
				payStream.setPayId(orderCode);
				payStreamMapper.insertSelective(payStream);
			}
			
			/*PayStreamExample payStreamExample = new PayStreamExample();
			Criteria payStreamCriteria = payStreamExample.createCriteria();
			payStreamCriteria.andOrderIdsStrEqualTo(orderIdStr);
			payStreamCriteria.andUserIdEqualTo(userId);
			List<PayStream> payStreams = payStreamMapper.selectByExample(payStreamExample);
			if(payStreams.size()>0){//支付流水已经创建过
				payStream = payStreams.get(0);
				if(payStream.getPayStatus() == 1){
					object.put("resultCode", "0");
					object.put("resultDesc", "该订单已经支付过啦，请勿重复支付");
					
					String serverB = AppSecretUtil.getAppSecret(userId, appA);			
					JSONObject dataObj = new JSONObject();	
					dataObj.put("serverB", serverB);			
					dataObj.put("payId", "");
					object.put("data",dataObj);
					
					return object.toString();
				}else{
					payStream.setPayTime(new Date());
					payId = payStream.getPayId();
				}
			}else{
				payStream.setPayStatus(0);
				//payStream.setPayType(2);
				payStream.setUserId(userId);
				payStream.setPayTime(new Date());
				payStream.setOrderIdsStr(orderIdStr);
				payId = UUID.randomUUID().toString().replace("-", "");
				payStream.setPayId(payId);
				payStreamMapper.insertSelective(payStream);
			}*/
			
			String bodyStr = "";
			String titleStr = "";
			//Double totalPay = 0.00;
			BigDecimal totalPay = new BigDecimal("0");

			Integer orderType = title.getOrderType();

			if (orderType == 1) {// 任务订单
				int taskId = title.getTaskInfoId();
				TaskInfo taskInfo = taskInfoMapper
						.selectByPrimaryKey(taskId);
				titleStr = "任务消费：";
				titleStr += taskInfo.getSellerInfoName();
				if (taskInfo.getUserId().equals(userId)) {
					String otherUserId = taskInfo.getOtherUserId();
					UserInfo userInfo = userInfoMapper
							.selectByPrimaryKey(otherUserId);
					bodyStr = DateTimeUtils.formatDateTime2String(
							taskInfo.getTaskAppointedTime(),
							"yyyy-MM-dd HH:mm")
							+ "和" + userInfo.getNickname() + "约会";
				} else {
					String otherUserId = taskInfo.getUserId();
					UserInfo userInfo = userInfoMapper
							.selectByPrimaryKey(otherUserId);
					bodyStr = DateTimeUtils.formatDateTime2String(
							taskInfo.getTaskAppointedTime(),
							"yyyy-MM-dd HH:mm")
							+ "和" + userInfo.getNickname() + "约会";
				}
			} else if (orderType == 2) {// 积分购买优惠券

			} else if (orderType == 3) {// 普通商家订单
				titleStr = "商家消费：";
				int sellerId = title.getSellerInfoId();
				SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId);
				bodyStr = DateTimeUtils.formatDateTime2String(title.getOrderTime(),"yyyy-MM-dd HH:mm")+" "+sellerInfo.getSellerName();
			} else if (orderType == 4) {// 任务评价打赏订单
				titleStr = "打赏：";
				String otherId = title.getOtherId();
				UserInfo userInfo = userInfoMapper.selectByPrimaryKey(otherId);
				bodyStr = DateTimeUtils.formatDateTime2String(title.getOrderTime(),"yyyy-MM-dd HH:mm")+" "+userInfo.getNickname();
			}else if (orderType == 6) {// 金币充值订单
				titleStr = "金币充值：";
				bodyStr = DateTimeUtils.formatDateTime2String(title.getOrderTime(),"yyyy-MM-dd HH:mm")+" "+title.getPayMoney();
			}else if (orderType == 7) {// 活动报名订单
				titleStr = "活动报名：";
				String acId = title.getParamId();
				ActivityInfo acInfo = activityInfoMapper.selectByPrimaryKey(acId);
				bodyStr = acInfo.getAcTitle()+"："+DateTimeUtils.formatDateTime2String(title.getOrderTime(),"yyyy-MM-dd HH:mm")+" "+title.getPayMoney();
			}else if(orderType == OrderType.PARTY_PREPAY){
				titleStr = "聚会预付款：";
				bodyStr = DateTimeUtils.formatDateTime2String(title.getOrderTime(),"yyyy-MM-dd HH:mm")+" "+title.getPayMoney();
			}else if(orderType == OrderType.PARTY_ORDER){
				titleStr = "聚会付费：";
				bodyStr = DateTimeUtils.formatDateTime2String(title.getOrderTime(),"yyyy-MM-dd HH:mm")+" "+title.getPayMoney();
			}else if(orderType == OrderType.OFFICAL_PARTY){
				titleStr = "官方聚会付费：";
				bodyStr = DateTimeUtils.formatDateTime2String(title.getOrderTime(),"yyyy-MM-dd HH:mm")+" "+title.getPayMoney();
			}
			
			//totalPay = ((double) title.getPayMoney()) / 100;
			totalPay = new BigDecimal(title.getPayMoney()).divide(new BigDecimal("100"));

			title.setPayType(1);
			title.setPayStatus(0);
			title.setPayStreamId(payId);
			orderInfoMapper.updateByPrimaryKeySelective(title);
					
			if (titleStr.length() > 100) {
				titleStr = titleStr.substring(0, 100);
			}
			
			payStream.setPayMoney(totalPay.doubleValue());
			payStream.setPayCode(UUID.randomUUID().toString().replace("-", ""));
			payStreamMapper.updateByPrimaryKey(payStream);
			
			String serverB = AppSecretUtil.getAppSecret(userId, appA);
			
			JSONObject dataObj = new JSONObject();
			
			dataObj.put("serverB", serverB);
			dataObj.put("payId", payId);
			
			object.put("data",dataObj);
	        object.put("resultCode",Constants.SUCCESS);
	        object.put("resultDesc","SUCCESS");
		} catch (Exception e) {
			object.put("resultCode",Constants.EXCEPTION);
			object.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
		}
		return object.toString();
	}
	
	/**
	 * 金币支付
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param payPass
	 * @param token
	 * @param payId
	 * @param appA
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/pay",produces="text/plain;charset=UTF-8")
	public String pay(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="payPass",required=true) String payPass,//32位密钥
			@RequestParam(value="token",required=true) String token,//32位密钥
			@RequestParam(value="payId",required=true) String payId,//支付流水id
			@RequestParam(value="appA",required=true) String appA  //交换参数
			) throws Exception{
		
		JSONObject object = new JSONObject();
		try {
			if(StringUtils.isBlank(payPass) || StringUtils.isBlank(token) || StringUtils.isBlank(payId) || appA == null){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				return object.toString();
			}
			
			PayStream payStream = payStreamMapper.selectByPrimaryKey(payId);
			if(payStream == null){
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);				
				return object.toString();
			}
			
			String userId = payStream.getUserId();
			
			if(AppSecretUtil.valAppToken(userId, token)){
				
			}else{
				object.put("resultCode", Constants.EXCEPTION);
				object.put("resultDesc", Constants.EXCEPTION_DESC);
				logger.info("serverB不匹配");
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			//验证支付密码是否正确
			UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
			String accountId=null;
			if (userVo.getAcountId()!=null) {
				accountId=userVo.getAcountId();
			}
			if(StringUtils.isBlank(accountId)){
				object.put("resultCode", Constants.FAIL);
				object.put("resultDesc", "您输入的支付密码不正确");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			UserAccount userAccount = userAccountMapper.selectByPrimaryKey(Integer.parseInt(accountId));
			if(userAccount == null){
				object.put("resultCode", Constants.FAIL);
				object.put("resultDesc", "您输入的支付密码不正确");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			if(StringUtils.isBlank(userAccount.getPayPassword())){
				object.put("resultCode", Constants.NOPAYPASS);
				object.put("resultDesc", "您还没有设置支付密码");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			//if(!userAccount.getPayPassword().equals(MDUtil.encodePwd(payPass))){
			if(!userAccount.getPayPassword().equals(payPass)){
				object.put("resultCode", Constants.FAIL);
				object.put("resultDesc", "您输入的支付密码不正确");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			Integer orderType = 0;
			Integer taskId = 0;
			
			PayStream stream = payStreamMapper.selectByPrimaryKey(payId);
			
			Integer gold = userAccount.getGold();
			if(gold <= 0 && payStream.getPayMoney() > 0){
				object.put("resultCode", Constants.FAIL);
				object.put("resultDesc", "您的剩余金币不足以支付，请充值或选择其他方式付款！");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);			
				object.put("data",dataObj);
				
				return object.toString();
			}
			if(gold < payStream.getPayMoney()*100){
				object.put("resultCode", Constants.FAIL);
				object.put("resultDesc", "您的剩余金币不足以支付，请充值或选择其他方式付款！");
				
				String serverB = AppSecretUtil.getAppSecret(userId, appA);			
				JSONObject dataObj = new JSONObject();	
				dataObj.put("serverB", serverB);
				object.put("data",dataObj);
				
				return object.toString();
			}
			
			OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(Long.parseLong(payStream.getOrderIdsStr()));
			
			BigDecimal payMoney = new BigDecimal(orderInfo.getPayMoney());
			
			userAccount.setGold(gold-orderInfo.getPayMoney());
			userAccountMapper.updateByPrimaryKeySelective(userAccount);
			
			OrderInfo info = null;
			OrderInfoExample orderExample = new OrderInfoExample();
			com.jsz.peini.bean.order.OrderInfoExample.Criteria orderCriteria = orderExample.createCriteria();
			orderCriteria.andPayStreamIdEqualTo(payId);
 			List<OrderInfo> titles = orderInfoMapper.selectByExample(orderExample);
			if(titles.size()>=0){
				for(OrderInfo title : titles){
					info = title;
					orderType = title.getOrderType();
					title.setOrderStatus(1);
					if(orderType == 1 || orderType == 3){
						title.setOrderStatus(1);
					}else{
						title.setOrderStatus(2);
					}
					title.setPayStatus(1);
					title.setPayTime(new Date());
					taskId = title.getTaskInfoId();
					orderInfoMapper.updateByPrimaryKeySelective(title);
					//boolean flag = orderInfoService.notifyPayOrder(title);
					
					RedisUtil.lpush(RedisConstants.ORDER_NOTIFY.getBytes(), SerializeUtil.serialize(title));
				}
			}
			stream.setPayType(1);
			stream.setFinishTime(new Date());
			stream.setPayStatus(1);
			payStreamMapper.updateByPrimaryKey(stream);
			
			GoldHistory history = new GoldHistory();
			history.setCreateTime(new Date());
			history.setHisId(UUID.randomUUID().toString().replace("-", ""));
			if(info.getOrderType() == 1){//任务消费
				history.setHisType(2);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(0);
				history.setParamId(String.valueOf(taskId));
				history.setUserId(userId);
				/*Double pMoney = payStream.getPayMoney();
				pMoney = pMoney * 100;
				history.setValNum(pMoney.longValue());*/
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
			}else if(info.getOrderType() == 3){//商家消费
				history.setHisType(4);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(0);
				history.setParamId(String.valueOf(info.getId()));
				history.setUserId(userId);
				/*Double pMoney = payStream.getPayMoney();
				pMoney = pMoney * 100;
				history.setValNum(pMoney.longValue());*/
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
			}else if(info.getOrderType() == 4){//打赏
				history.setHisType(5);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(0);
				history.setParamId(String.valueOf(taskId));
				history.setUserId(userId);
				/*Double pMoney = payStream.getPayMoney();
				pMoney = pMoney * 100;
				history.setValNum(pMoney.longValue());*/
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
				
				history = new GoldHistory();
				history.setCreateTime(new Date());
				history.setHisId(UUID.randomUUID().toString().replace("-", ""));
				history.setHisType(6);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(1);
				history.setParamId(String.valueOf(taskId));
				history.setUserId(info.getOtherId());
				/*history.setValNum(pMoney.longValue());*/
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
				
				String otherId = info.getOtherId();
				UserAccount account = userAccountMapper.selectByUserId(otherId);				
				account.setGold(account.getGold()+payMoney.intValue());
				int updatenum=userAccountMapper.updateByPrimaryKeySelective(account);
				if (updatenum>0) {
					
					BigDecimal valNum = new BigDecimal(history.getValNum());
					valNum = valNum.divide(new BigDecimal("100"));
					
					//消息推送
					UserInfo newuserInfo = userInfoMapper.selectByPrimaryKey(userId);
					JSONObject object2 = new JSONObject();
					object2.put("title", "系统消息");
					object2.put("content", "恭喜！“"+PeiniUtils.updateNickName(newuserInfo.getNickname(), newuserInfo.getAccCode())+"”赠送了您"+valNum+"金币，快去我的财富查看吧~点击可查看");
					object2.put("type", 111);
					UserInfo newuserInfo2 = userInfoMapper.selectByPrimaryKey(info.getOtherId());
					AppPush.sendPush(object2,newuserInfo2.getUserPhone().split(","));
					
					//给发起人推送消息
					SmsSendUtil.sendSmsNotify(info.getOtherId(), info.getId().toString(), 111, "恭喜！“"+PeiniUtils.updateNickName(newuserInfo.getNickname(), newuserInfo.getAccCode())+"”赠送了您"+valNum+"金币，快去我的财富查看吧~点击可查看");
				}
			}else if(info.getOrderType() == 5){//金币转赠
				history.setHisType(7);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(0);
				history.setParamId(String.valueOf(info.getId()));
				history.setUserId(userId);
				history.setOtherid(info.getOtherId());
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
				
				history = new GoldHistory();
				history.setCreateTime(new Date());
				history.setHisId(UUID.randomUUID().toString().replace("-", ""));
				history.setHisType(8);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(1);
				history.setOtherid(userId);
				history.setParamId(String.valueOf(info.getId()));
				history.setUserId(info.getOtherId());
				//history.setValNum(pMoney.longValue());
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
				
				String otherId = info.getOtherId();
				UserAccount account = userAccountMapper.selectByUserId(otherId);
				account.setGold(account.getGold()+payMoney.intValue());
				userAccountMapper.updateByPrimaryKeySelective(account);
				
				BigDecimal valNum = new BigDecimal(history.getValNum());
				valNum = valNum.divide(new BigDecimal("100"));
				
				//消息推送
				UserInfo newuserInfo = userInfoMapper.selectByPrimaryKey(userId);
				JSONObject object2 = new JSONObject();
				object2.put("title", "系统消息");
				object2.put("type", 112);
				object2.put("content", "恭喜！“"+PeiniUtils.updateNickName(newuserInfo.getNickname(), newuserInfo.getAccCode())+"”赠送了您"+valNum+"金币，快去我的财富查看吧~点击可查看");
				UserInfo newuserInfo2 = userInfoMapper.selectByPrimaryKey(otherId);
				AppPush.sendPush(object2,newuserInfo2.getUserPhone().split(","));
				object2.put("content", "转账成功！"+valNum+"金币已成功飞入“"+PeiniUtils.updateNickName(newuserInfo2.getNickname(), newuserInfo2.getAccCode())+"”的财富中~点击可查看");
				AppPush.sendPush(object2,newuserInfo.getUserPhone().split(","));
				
				SmsSendUtil.sendSmsNotify(otherId, orderInfo.getId().toString(), 112, "恭喜！“"+PeiniUtils.updateNickName(newuserInfo.getNickname(), newuserInfo.getAccCode())+"”赠送了您"+valNum+"金币，快去我的财富查看吧~点击可查看");
				
				SmsSendUtil.sendSmsNotify(userId, orderInfo.getId().toString(), 112, "转账成功！"+valNum+"金币已成功飞入“"+PeiniUtils.updateNickName(newuserInfo2.getNickname(), newuserInfo2.getAccCode())+"”的财富中~点击可查看");
				
			}else if(info.getOrderType() == 7){//活动报名
				history.setHisType(10);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(0);
				history.setParamId(info.getParamId());
				history.setUserId(userId);
				/*Double pMoney = payStream.getPayMoney();
				pMoney = pMoney * 100;
				history.setValNum(pMoney.longValue());*/
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
			}else if(info.getOrderType() == OrderType.PARTY_PREPAY){ //聚会预付款
				history.setHisType(13);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(0);
				history.setParamId(info.getParamId());
				history.setUserId(userId);
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
				partyService.setUserPrePayed(orderInfo);
			}else if(info.getOrderType() == OrderType.PARTY_ORDER || info.getOrderType() == OrderType.OFFICAL_PARTY){  //聚会付款,官方聚会付款
				history.setHisType(info.getOrderType() == OrderType.OFFICAL_PARTY ? 15 : 14);
				history.setIsDel(1);
				history.setIsFinish(1);
				history.setOpType(0);
				history.setParamId(info.getParamId());
				history.setUserId(userId);
				history.setValNum(payMoney.longValue());
				goldHistoryMapper.insertSelective(history);	
				partyService.setUserPrePayed(orderInfo);
			}
			
			JSONObject dataObj = new JSONObject();
			String serverB = AppSecretUtil.getAppSecret(userId, appA);
			dataObj.put("serverB", serverB);
			
	        object.put("resultCode",Constants.SUCCESS);
	        object.put("resultDesc","SUCCESS");
	        object.put("orderId", orderInfo.getId());
	        object.put("data",dataObj);
		} catch (Exception e) {
			object.put("resultCode", Constants.EXCEPTION);
			object.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
			return object.toString();
		}
		return object.toString();
	}

	/**
	 * 获取金币兑换规则列表
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getConversionList",produces="text/plain;charset=UTF-8")
	public String getConversionList(HttpServletRequest request,HttpServletResponse response
			) throws Exception{
		
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		try {
			ConversionInfoExample example = new ConversionInfoExample();
			com.jsz.peini.bean.order.ConversionInfoExample.Criteria criteria = example.createCriteria();
			criteria.andStartTimeLessThan(new Date());
			criteria.andEndTimeGreaterThan(new Date());
			example.setOrderByClause("priority desc");
			List<ConversionInfo> list = conversionInfoMapper.selectByExample(example);
			if(list.size()>0){
				ConversionInfo conversionInfo = list.get(0);
				ConversionDetailExample detailExample = new ConversionDetailExample();
				com.jsz.peini.bean.order.ConversionDetailExample.Criteria detCriteria = detailExample.createCriteria();
				detCriteria.andConversionInfoIdEqualTo(conversionInfo.getId());
				detailExample.setOrderByClause("location_num asc");
				List<ConversionDetail> details = conversionDetailMapper.selectByExample(detailExample);
				for (ConversionDetail detail : details) {
					logger.info(detail.getLocationNum());
					JSONObject obj = new JSONObject();
					obj.put("id", detail.getId());
					obj.put("payNum", ((double)detail.getPayNum())/100);
					obj.put("goldNum", detail.getGoldNum());
					array.add(obj);
				}
			}
			object.put("data", array);
			object.put("resultCode", Constants.SUCCESS);
			object.put("resultDesc", Constants.SUCCESS_DESC);
		} catch (Exception e) {
			object.put("resultCode", Constants.EXCEPTION);
			object.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
			return object.toString();
		}
		return object.toString();
	}
}
